import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Distance
 */
public class Distance {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        /* cityname 所有城市名 */
        ArrayList<String> cityname = new ArrayList<String>();
        /* map 城市名到城市引索的对应关系 */
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        String name = new String();

        name = in.next();
        while (!name.equals("###")) {
            cityname.add(name);
            /* 构造映射 */
            map.put(name, cityname.size() - 1);
            name = in.next();

        }
        /* distances[i][j] ->引索为i的城市到引索为j的城市的距离 */
        int[][] distances = new int[cityname.size()][cityname.size()];

        for (int j = 0; j < cityname.size(); j++) {
            
            for (int i = 0; i < cityname.size(); i++) {
                distances[j][i] = in.nextInt();
            }

        }
        int city1 = map.get(in.next());
        int city2 = map.get(in.next());
        System.out.println(distances[city1][city2]);
        in.close();
    }
}